package dhbw.GUI.Panels;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;

import Main.MyController;

import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;

import dhbw.Data.Language;
import dhbw.Data.Settings;

public class PanelChangePassword extends JPanel implements ActionListener {

	private MyController myc;
	private JPasswordField pf_passwordOld;
	private JPasswordField pf_passwordNew;
	private JPasswordField pf_passwordNewConfirm;
	private JButton bt_save;
	private JButton bt_cancel;
	private JFrame parent;

	public PanelChangePassword(MyController myc, JFrame parent) {
		this.myc = myc;
		this.parent = parent;
		build();
	}

	/**
	 * Build Panel
	 */
	private void build() {
		this.setBackground(Settings.BG_COLOR);
		bt_save = new JButton(Language.SAVE[myc.getLanguage()]);
		bt_save.addActionListener(this);
		bt_cancel = new JButton(Language.CANCEL[myc.getLanguage()]);
		bt_cancel.addActionListener(this);
		pf_passwordOld = new JPasswordField();
		pf_passwordNew = new JPasswordField();
		pf_passwordNewConfirm = new JPasswordField();

		CellConstraints cc = new CellConstraints();
		String cols = "16dlu,pref,6dlu,pref,16dlu";
		String rows = "6dlu,pref,6dlu,pref,6dlu,pref,6dlu,pref,6dlu";
		FormLayout grid = new FormLayout(cols, rows);
		this.setLayout(grid);
		this.add(new JLabel(Language.OLDPASSWORD[myc.getLanguage()]),
				cc.xy(2, 2));
		this.add(new JLabel(Language.NEWPASSWORD[myc.getLanguage()]),
				cc.xy(2, 4));
		this.add(new JLabel(Language.NEWPASSWORDCONFIRM[myc.getLanguage()]),
				cc.xy(2, 6));
		this.add(pf_passwordOld, cc.xy(4, 2));
		this.add(pf_passwordNew, cc.xy(4, 4));
		this.add(pf_passwordNewConfirm, cc.xy(4, 6));
		this.add(bt_save, cc.xy(2, 8));
		this.add(bt_cancel, cc.xy(4, 8));
	}

	@Override
	/**
	 * Click-Listener for Panel "Change Password"
	 */
	public void actionPerformed(ActionEvent arg0) {
		if (arg0.getSource().equals(bt_save)) {

			StringBuilder pwOld = new StringBuilder();
			for (int i = 0; i < pf_passwordOld.getPassword().length; i++) {
				pwOld.append(pf_passwordOld.getPassword()[i]);
			}
			StringBuilder pwNew = new StringBuilder();
			for (int i = 0; i < pf_passwordNew.getPassword().length; i++) {
				pwNew.append(pf_passwordNew.getPassword()[i]);
			}
			StringBuilder pwNewConfirm = new StringBuilder();
			for (int i = 0; i < pf_passwordNewConfirm.getPassword().length; i++) {
				pwNewConfirm.append(pf_passwordNewConfirm.getPassword()[i]);
			}

			boolean done = myc.getActiveUser()
					.passwortAendern(pwOld.toString(), pwNew.toString(), pwNewConfirm.toString());
			if (done) {
				myc.saveAll();
				JOptionPane
						.showMessageDialog(null,
								Language.PASSWORDCHANGEDSUCCESSFULLY[myc
										.getLanguage()]);
				parent.dispose();
				myc.logout();
			} else {
				JOptionPane.showMessageDialog(null,
						Language.PASSWORDNOTCHANGEDERROR[myc.getLanguage()]);
				// reset fields
			}
		}
		if (arg0.getSource().equals(bt_cancel)) {
			parent.dispose();

		}
	}

}
